package com.alex.game.framework
{
	import Box2D.Collision.Shapes.b2CircleDef;
	import Box2D.Collision.Shapes.b2PolygonDef;
	import Box2D.Collision.b2AABB;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2DebugDraw;
	import Box2D.Dynamics.b2World;
	
	import com.unsk.item.Box2DBodyContactListener;
	
	import flash.display.Sprite;

	public class WorldFabrique
	{
		//Monde
		public var world:b2World;
		
		//Liste des objets créé
		private var body_list:Array = new Array();
		
		public  function createWorld(debugSprite:Sprite = null):void
		{
			
			// set debug draw
			if(debugSprite != null){
				var dbgDraw:b2DebugDraw = new b2DebugDraw();
				dbgDraw.m_sprite = debugSprite;
				dbgDraw.m_drawScale = 30.0;
				dbgDraw.m_fillAlpha = 1;
				dbgDraw.m_lineThickness = 1.0;
				dbgDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit;
			}
			//Créé les limites du monde
			var worldAABB:b2AABB = new b2AABB();
			worldAABB.lowerBound.Set(-3000/GG.RATIO,-3000/GG.RATIO);
			worldAABB.upperBound.Set(3000/GG.RATIO,3000/GG.RATIO);
			
			//Créé la gravité
			var gravity:b2Vec2 = new b2Vec2(0,20);
			var doSleep:Boolean = true;

			//Créé le monde
			world = new b2World(worldAABB, gravity, doSleep);

			//Active le debug sur le monde
			if(debugSprite != null)
				world.SetDebugDraw(dbgDraw);
		}
		public function destroyWorld():void
		{
			
		}
		public function update(step:Number, itterator:Number):void
		{
			world.Step(step, itterator);
		}
		/**
		 * Créé un corps impossible à bouger
		 * @param posX
		 * @param posY
		 * @param width
		 * @param height
		 * 
		 */		
		public function addWall(posX:Number, posY:Number,width:Number, height:Number):void
		{
			var groundBodyDef:b2BodyDef = new b2BodyDef();
			groundBodyDef.position.Set(((posX)+(width/2))/GG.RATIO, ((posY)+(height/2))/GG.RATIO);
			
			var grounBody:b2Body = world.CreateBody(groundBodyDef);
			
			var groundShapDef:b2PolygonDef = new b2PolygonDef();
			groundShapDef.SetAsBox((width/2)/GG.RATIO,(height/2)/GG.RATIO);
			grounBody.CreateShape(groundShapDef);
		}
		/**
		 * Cne créé un corps avec une densité et une friction, sensible aux forces du monde
		 * @param posX
		 * @param posY
		 * @param width
		 * @param height
		 * @param density
		 * @param friction
		 * 
		 */		
		public function addBox(posX:Number, posY:Number, width:Number, height:Number,
		density:Number=1, friction:Number=0.2, restitution:Number=0.3):b2Body
		{
			var bodyDef:b2BodyDef = new b2BodyDef();
			bodyDef.position.Set(((posX)+(width/2))/GG.RATIO, ((posY)+(height/2))/GG.RATIO);
			
			var body:b2Body = world.CreateBody(bodyDef);
			
			var shapeDef:b2PolygonDef = new b2PolygonDef();
			shapeDef.SetAsBox((width/2)/GG.RATIO,(height/2)/GG.RATIO);
			shapeDef.density = density;
			shapeDef.friction = friction;
			shapeDef.restitution = restitution;
			body.CreateShape(shapeDef);
			body.SetMassFromShapes();
			
			return body;
		}
		/**
		 * 
		 * @param posX
		 * @param posY
		 * @param radius
		 * @param static
		 * @param density
		 * @param friction
		 * @param restitution
		 * @return 
		 * 
		 */		
		public function addCircle(posX:Number, posY:Number, radius:Number,
			density:Number=1, friction:Number=0.3, restitution:Number=0.3):b2Body {
			/* Following Code creates a circle */
			var bodyDef:b2BodyDef = new b2BodyDef();
			bodyDef.position.Set(((posX)+(radius/2))/GG.RATIO, ((posY)+(radius/2))/GG.RATIO);
			var circleDef:b2CircleDef = new b2CircleDef();//Defines circleDef as a new b2CircleDef
			circleDef.radius = radius/GG.RATIO/2;//Sets the radius of the ball
			circleDef.density = density;
			circleDef.friction = friction;
			circleDef.restitution = restitution;
			var body:b2Body = world.CreateBody(bodyDef);
			body.CreateShape(circleDef);
			body.SetMassFromShapes();
			
			return body;
			
		}
	}
}